package org.x.user.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.lang.Boolean;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.x.common.base.result.Result;
import org.x.user.entity.SysMenu;
import org.x.user.dto.CreateGroup;
import org.x.user.dto.DeleteGroup;
import org.x.user.dto.MenuDTO;
import org.x.user.dto.UpdateGroup;
import org.x.user.query.MenuQuery;
import org.x.user.query.QueryGroup;
import org.x.user.service.MenuService;
import org.x.user.vo.MenuTreeVO;

/**
 * 菜单管理
 *
 * @author 夏包包
 * @date 2025-03-08 13:39:03
 */
@Tag(name = "菜单管理")
@RequestMapping("menu")
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 新增菜单:检查菜单是否已存在，如果不存在则保存菜单信息到数据库
     *
     * @param menuDTO 菜单管理入参实体
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Operation(summary = "新增菜单:检查菜单是否已存在，如果不存在则保存菜单信息到数据库")
    public Result<Boolean> addMenu(@RequestBody @Validated(CreateGroup.class) MenuDTO menuDTO) {
        Boolean result = menuService.addMenu(menuDTO);
        return Result.success(result);
    }

    /**
     * 删除菜单:检查菜单是否存在，如果存在则删除菜单信息
     *
     * @param id 菜单管理入参实体
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @Operation(summary = "删除菜单:检查菜单是否存在，如果存在则删除菜单信息")
    public Result<Boolean> deleteMenu(@PathVariable String id) {
        Boolean result = menuService.removeById(id);
        return Result.success(result);
    }

    /**
     * 修改菜单:检查菜单是否存在，如果存在则更新菜单信息
     *
     * @param menuDTO 菜单管理入参实体
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @Operation(summary = "修改菜单:检查菜单是否存在，如果存在则更新菜单信息")
    public Result<Boolean> updateMenu(@RequestBody @Validated(UpdateGroup.class) MenuDTO menuDTO) {
        Boolean result = menuService.updateMenu(menuDTO);
        return Result.success(result);
    }

    /**
     * 查询菜单:根据菜单ID查询菜单信息
     *
     * @param menuQuery 菜单查询入参实体
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @Operation(summary = "查询菜单:根据菜单ID查询菜单信息")
    public Result<SysMenu> getMenuInfo(@Validated(QueryGroup.class) MenuQuery menuQuery) {
        SysMenu result = menuService.getMenuInfo(menuQuery);
        return Result.success(result);
    }
//
//    /**
//     * 查询菜单层次关系:根据菜单ID查询其所有后代菜单及其深度
//     *
//     * @param menuQuery 菜单查询入参实体
//     * @return
//     */
//    @RequestMapping(value = "/closure", method = RequestMethod.GET)
//    @Operation(summary = "查询菜单层次关系:根据菜单ID查询其所有后代菜单及其深度")
//    public Result<List<MenuClosureDO>> getMenuClosure(@Validated(QueryGroup.class) MenuQuery menuQuery) {
//        List<MenuClosureDO> result = menuService.getMenuClosure(menuQuery);
//        return Result.success(result);
//    }

    /**
     * 查询树形结构的菜单
     */
    @GetMapping("/tree")
    @Operation(summary = "查询树形结构的菜单")
    public Result getMenuTree() {
        List<MenuTreeVO> result = menuService.getMenuTree();
        return Result.success(result);
    }


}
